/**
 * @file    app_ems_comm.c
 * @brief   用户任务-EMS通信任务
 * @author  name
 * @version 1.0.1
 * @date    2023-04-25
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-25 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */
/* includes ------------------------------------------------------------------*/
#include "app_com_comm.h"
#include "FreeRTOS.h"
#include "task.h"
#include "If_com.h"
#include "parallel_com.h"
#include "iap_boot.h"
#include "boot_param.h"
#include "cfg_prj.h"
#include "voltronic_rs485.h"
#include "growatt_rs485.h"
#include "app_monitor.h"
#include "var_mng.h"
#include "parallel.h"
#include "pc_jbd.h"
#include "If_di.h"
#include "pcs_rs485_mng.h"
#include "iap_master.h"
#include "jbd_modbus.h"
#include "pc_pace.h"
#include "wifi_blue.h"
#include "dev_afe.h"
#include "param_mng.h"
#include "parallel_pace.h"
/* macro/define --------------------------------------------------------------*/
#define APP_COMM_TASK_CYCLE          (30)
#define SERIANET_TIMEOUT             (1000)
#define SEND_MIN_CYCLE               (5000)
/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/
/* static variables ----------------------------------------------------------*/
static com_data_type comm_recv_buf = {0};
static serialnet_data_type serianet_data = {0};
static uint8_t update_slave_flag = 0;
static uint16_t update_slave_cnt = 0;
static uint16_t com_reset_cnt[COM_ID_MAX] = {0};  //
static uint32_t com_init_cnt[COM_ID_MAX] = {0};
static uint8_t broadcase_cmd_cnt = 0;   // 广播发送次数
static uint8_t broadcase_cmd_flag = 0;   // 广播发送次数

static uint32_t wifi_send_clcye = 0;
uint8_t wifi_send_flag = 0;
static uint16_t send_time_delay = 0xffff;
/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
 * @brief  PCS通信任务 
 * @param  pvParameters
 */
void vComCommTask(void *pvParameters)
{
	uint16_t i = 0;
	uint32_t task_cycle = 0;
    uint16_t timeout_cnt = 0;
	If_com_init_drv(COM_PARALLEL , COM_PARALLEL_RATE);
	If_com_init_drv(COM_PCS , pcs_rs485_get_com_rate());
	If_com_init_drv(COM_LCD , COM_LCD_RATE);
	If_com_init_drv(COM_BLUE , COM_BLUE_RATE);
	If_com_init_drv(COM_GPS, COM_GPS_RATE);
	If_com_init_drv(COM_RS232 , COM_RS232_RATE);
	
   	task_run_reset(TASK_ID_COM_COMM);
    while(1)
    {
		task_cycle = task_get_cycle(TASK_ID_COM_COMM);
		if(task_cycle > 50 * APP_COMM_TASK_CYCLE)
			task_cycle = APP_COMM_TASK_CYCLE;
		/*------------------------------------------------------------------------------
									并机协议及透传协议处理
		  ----------------------------------------------------------------------------*/
#ifdef USE_MASTER_UPGARDE_SLAVE
		if((TRUE == iap_master_is_upgrade_slave() && (PARALLEL_MASTER_ADDR == var_get_data(VAR_ID_SELF_ADDR))))
		{
			iap_master_run_upgrade_slave(APP_COMM_TASK_CYCLE , COM_PARALLEL);
		}
		else
#endif
		{
			if(update_slave_flag)
			{
				update_slave_cnt += task_cycle;
				if(update_slave_cnt >= 5000)
				{
					update_slave_flag = 0;
					update_slave_cnt = 0;
				}
			}
			
			if((PAL_MODE_SW_RS485 == parallel_get_mode()) || (FALSE == parallel_com_auto_addr(COM_PARALLEL , APP_COMM_TASK_CYCLE)))
			{	
				if(PARALLEL_MASTER_ADDR == var_get_data(VAR_ID_SELF_ADDR))  /* 如果为主机 则加载主机协议 */
				{
					if((0 == serianet_data.recv_flag) && (0 == update_slave_flag) && (broadcase_cmd_flag == 0))
					{
						if(TRUE == parallel_com_master_deal(APP_COMM_TASK_CYCLE , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
						{
							If_com_send_str(COM_PARALLEL , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
						}
					}
					else
					{
						if(broadcase_cmd_flag)
						{
							serianet_data.timeout_cnt += task_cycle;
							if(serianet_data.timeout_cnt >= 300)
							{
								serianet_data.timeout_cnt = 0;
								If_com_send_str(COM_PARALLEL , serianet_data.tx_buf, serianet_data.tx_num);	
								++ broadcase_cmd_cnt;
								if(broadcase_cmd_cnt >= 3)
								{
									broadcase_cmd_flag = 0;
									broadcase_cmd_cnt = 0;
								}
							}
						}
						else
						{
							if(0 == update_slave_flag)
							{
								serianet_data.timeout_cnt += task_cycle;
								if(serianet_data.timeout_cnt >= SERIANET_TIMEOUT)
								{
									serianet_data.recv_flag = 0;
									serianet_data.send_flag = 0;
									serianet_data.timeout_cnt = 0;
								}
								else if((serianet_data.timeout_cnt >= 300) && (0 == serianet_data.send_flag))
								{
									serianet_data.send_flag = 1;
									If_com_send_str(COM_PARALLEL , serianet_data.tx_buf, serianet_data.tx_num);	
								}
								else
								{
								   ;
								}
							}
							else
							{
								if(serianet_data.send_flag == 0)
								{
									serianet_data.send_flag = 1;
									If_com_send_str(COM_PARALLEL , serianet_data.tx_buf, serianet_data.tx_num);	
								}
							}
						}
					}
				}
				else
				{
					// 定时更新从机数据
					parallel_slave_update_self(task_cycle);
				}
			}
			/*------------------------------------------------------------------------------
						并机接口数据处理
			----------------------------------------------------------------------------*/
			if(TRUE == If_com_recv_comp(COM_PARALLEL))
			{
				comm_recv_buf.rx_num = If_com_get_recv_num(COM_PARALLEL);
				If_com_get_recv_buf(COM_PARALLEL , comm_recv_buf.rx_buf , comm_recv_buf.rx_num);
				/* 解析是否为透传协议 */
				if(1 == serianet_data.send_flag)
				{
				//	if(TRUE == parallel_bst_analysis_recv(com_ems_data.rx_buf , com_ems_data.rx_num,serianet_data))
					{
						serianet_data.recv_flag = 0;
						serianet_data.send_flag = 2;
						serianet_data.timeout_cnt = 0;
						if(TRUE == is_update_slave_cmd(comm_recv_buf.rx_buf , comm_recv_buf.rx_num))
						{
							update_slave_flag = 1;
							update_slave_cnt = 0;
						}
						if(TRUE == is_update_reply_slave_cmd(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num , pcs_rs485_get_com_rate()))
						{
							If_com_send_str(COM_BLUE , comm_recv_buf.tx_buf , comm_recv_buf.tx_num);
						}
						else
						{
							If_com_send_str(COM_BLUE , comm_recv_buf.rx_buf, comm_recv_buf.rx_num);
						}
					}
				}
				
				if(PARALLEL_MASTER_ADDR == var_get_data(VAR_ID_SELF_ADDR))
				{
					if(TRUE == is_update_slave_cmd(comm_recv_buf.rx_buf , comm_recv_buf.rx_num))
					{
						update_slave_flag = 1;
						update_slave_cnt = 0;
					}
				}
				
				if(TRUE == is_iap_upgrade_cmd(comm_recv_buf.rx_buf , comm_recv_buf.rx_num))
				{
					comm_recv_buf.tx_num = iap_boot_reply_upgrade_cmd(comm_recv_buf.tx_buf , COM_PARALLEL_RATE , COM_MAX_PACK_SIZE);
					If_com_send_str(COM_PARALLEL , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
					iap_boot_jump_before_deal(BOOT_UPGRADE_PRALLEL);
				}
				/* 上位机通信数据 */
				else if(TRUE == parallel_com_recv_deal(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num , 1))
				{
					If_com_send_str(COM_PARALLEL , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
				}
				else if(TRUE == parallel_pace_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
				{
					If_com_send_str(COM_PARALLEL , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
				}
				else
				{
					if((PAL_MODE_SW_RS485 == parallel_get_mode()) || (PAL_MODE_AUTO_RS485 == parallel_get_mode()))
					{
						if(PARALLEL_MASTER_ADDR == var_get_data(VAR_ID_SELF_ADDR))
							parallel_com_read_slave_data(comm_recv_buf.rx_buf, comm_recv_buf.rx_num);
					}
					else
					{

					}
				}
				
				var_set_data(VAR_ID_COMM_OFF_CNT , 0);
				var_set_data(VAR_ID_COMM_STATE , 0);
				com_reset_cnt[COM_PARALLEL] = 0;
				com_init_cnt[COM_PARALLEL] = 0;
				If_com_start_recv(COM_PARALLEL);
			}
		}
		/*------------------------------------------------------------------------------
                            日月元RS485协议处理 - 上位机通信
		  ----------------------------------------------------------------------------*/
		if(TRUE == If_com_recv_comp(COM_PCS))
		{ 
			comm_recv_buf.rx_num = If_com_get_recv_num(COM_PCS);
            If_com_get_recv_buf(COM_PCS , comm_recv_buf.rx_buf , comm_recv_buf.rx_num);
			/* PCS 通信协议处理 */
			if(TRUE == pc_pace_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
			{
				If_com_send_str(COM_PCS , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}
			else if(TRUE == pcs_rs485_comm_process(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
				If_com_send_str(COM_PCS , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
            /* 上位机通信协议 - 检测是否为升级指令 */
			else if(TRUE == is_iap_upgrade_cmd(comm_recv_buf.rx_buf , comm_recv_buf.rx_num))
			{
				comm_recv_buf.tx_num = iap_boot_reply_upgrade_cmd(comm_recv_buf.tx_buf , COM_PCS_BOOT_RATE , 1024);
				If_com_send_str(COM_PCS , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
                iap_boot_jump_before_deal(BOOT_UPGRADE_PCS);
			}
            /* 上位机通信数据 - 读写命令 */
            else if(TRUE == parallel_com_recv_deal(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num , 1))
            {
                If_com_send_str(COM_PCS , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
            }
			else if(TRUE == pc_jbd_com_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
			{
				If_com_send_str(COM_PCS , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}
			else if(TRUE == jbd_modbus_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
			{
				If_com_send_str(COM_PCS , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}
			else if(wifi_blue_com_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
			{
				If_com_send_str(COM_PCS , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}
			else
			{
				
			}
                
			var_set_data(VAR_ID_COMM_OFF_CNT , 0);
			var_set_data(VAR_ID_COMM_STATE , 0);
			
			com_reset_cnt[COM_PCS] = 0;
			com_init_cnt[COM_PCS] = 0;
			
			If_com_start_recv(COM_PCS);
		}
		
        /*------------------------------------------------------------------------------
                            LCD通信接口-协议处理
		  ----------------------------------------------------------------------------*/
		if(TRUE == If_com_recv_comp(COM_LCD))
		{
			comm_recv_buf.rx_num = If_com_get_recv_num(COM_LCD);
            If_com_get_recv_buf(COM_LCD , comm_recv_buf.rx_buf , comm_recv_buf.rx_num);
			if(TRUE == is_iap_upgrade_cmd(comm_recv_buf.rx_buf , comm_recv_buf.rx_num))
			{
				comm_recv_buf.tx_num = iap_boot_reply_upgrade_cmd(comm_recv_buf.tx_buf , COM_BLUE_BOOT_RATE , COM_MAX_PACK_SIZE);
				If_com_send_str(COM_LCD , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
                iap_boot_jump_before_deal(BOOT_UPGRADE_LCD);
			}
            /* 上位机通信数据 - 读写命令 */
            else if(TRUE == parallel_com_recv_deal(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num , 0))
            {
                If_com_send_str(COM_LCD , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
            }
			else if(TRUE == jbd_modbus_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
			{
				If_com_send_str(COM_LCD , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}
			else if(TRUE == pc_jbd_com_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
			{
				If_com_send_str(COM_LCD , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}
			else if(wifi_blue_com_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
			{
				If_com_send_str(COM_LCD , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}
                
			com_reset_cnt[COM_LCD] = 0;
			com_init_cnt[COM_LCD] = 0;
			If_com_start_recv(COM_LCD);
		}
		/*------------------------------------------------------------------------------
                                   蓝牙协议通信接口处理
		  ----------------------------------------------------------------------------*/
		if(TRUE == If_com_recv_comp(COM_BLUE))
		{
			comm_recv_buf.rx_num = If_com_get_recv_num(COM_BLUE);
            If_com_get_recv_buf(COM_BLUE , comm_recv_buf.rx_buf , comm_recv_buf.rx_num);
			uint8_t remote_src = 0;
			/* 上位机通信协议 - 检测是否为升级指令 */
			remote_src = is_remote_upgrade_cmd(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num);
			if(REMOTE_UPGRADE_NULL != remote_src)
			{
				If_com_send_str(COM_BLUE , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}
            else if(TRUE == parallel_com_recv_deal(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num , 1))
            {
				// 主机才识别透传
				if(comm_recv_buf.rx_buf[0] == BROADCASE_ADDR)
				{
					if(PARALLEL_MASTER_ADDR == var_get_data(VAR_ID_SELF_ADDR))
					{
						broadcase_cmd_flag = 1;
						broadcase_cmd_cnt = 0;
						serianet_data.timeout_cnt = 0;
						serianet_data.tx_num = comm_recv_buf.rx_num;
						for( i = 0;(i < comm_recv_buf.rx_num) && (i < SERIANET_BUFF_SIZE); ++ i)
						{
							serianet_data.tx_buf[i] = comm_recv_buf.rx_buf[i];
						}
						If_com_send_str(COM_BLUE , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
					}
					else
					{
						;
					}
				}
				else
				{
					If_com_send_str(COM_BLUE , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
				}
            }			
			else if(TRUE == is_iap_old_upgrade_cmd(comm_recv_buf.rx_buf , comm_recv_buf.rx_num))
			{
				comm_recv_buf.tx_num = iap_boot_reply_old_upgrade_cmd(comm_recv_buf.tx_buf , COM_BLUE_BOOT_RATE , COM_MAX_PACK_SIZE);
				If_com_send_str(COM_BLUE , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
                iap_boot_jump_before_deal(BOOT_UPGRADE_BLUE);
			}/* 上位机通信数据 - 读写命令 */
			else if(TRUE == is_iap_upgrade_no_addr_cmd(comm_recv_buf.rx_buf , comm_recv_buf.rx_num))
			{
				comm_recv_buf.tx_num = iap_boot_reply_upgrade_cmd(comm_recv_buf.tx_buf , COM_BLUE_BOOT_RATE , COM_MAX_PACK_SIZE);
				If_com_send_str(COM_BLUE , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
                iap_boot_jump_before_deal(BOOT_UPGRADE_BLUE);
			}/* 上位机通信数据 - 读写命令 */
			else if(REMOTE_UPGRADE_NULL != is_remote_upgrade_cmd(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
			{
				If_com_send_str(COM_BLUE , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}

			else if(TRUE == pc_pace_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
            {
                If_com_send_str(COM_BLUE , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
            }
			else if(TRUE == pc_jbd_com_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
			{
				If_com_send_str(COM_BLUE , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}
			else if(TRUE == jbd_modbus_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
			{
				If_com_send_str(COM_BLUE , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}
			else if(wifi_blue_com_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
			{
				If_com_send_str(COM_BLUE , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}/* 如果是主机 则检测是否为透传协议 */
			else if(PARALLEL_MASTER_ADDR == var_get_data(VAR_ID_SELF_ADDR))
			{
                 if(TRUE == parallel_com_serialnet(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , &serianet_data))
				 {
					serianet_data.recv_flag = 1;
					serianet_data.send_flag = 0;
                    serianet_data.timeout_cnt = 0;
					serianet_data.tx_num = comm_recv_buf.rx_num;
					for( i = 0;(i < comm_recv_buf.rx_num) && (i < SERIANET_BUFF_SIZE); ++ i)
					{
						serianet_data.tx_buf[i] = comm_recv_buf.rx_buf[i];
					}
					if(TRUE == is_update_slave_cmd(comm_recv_buf.rx_buf , comm_recv_buf.rx_num))
					{
						update_slave_flag = 1;
						update_slave_cnt = 0;
					}
				}
			}
             
			wifi_send_clcye = 0;
			com_reset_cnt[COM_BLUE] = 0;	
			com_init_cnt[COM_BLUE] = 0;				
			If_com_start_recv(COM_BLUE);
		}
		else
		{
			if(0x5AA5 == param_get_data(PAR_ID_WIFI_REP_EN))
			{
				wifi_send_clcye += task_cycle;
				send_time_delay += task_cycle;
				if(send_time_delay >= SEND_MIN_CYCLE)
				{
					send_time_delay = SEND_MIN_CYCLE;
					if((wifi_send_flag) || (wifi_send_clcye >= param_get_data(PAR_ID_WIFI_CYCLE) * 1000))
					{
						com_report_total_msg(comm_recv_buf.tx_buf , &comm_recv_buf.tx_num);
						If_com_send_str(COM_BLUE , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
						
						wifi_send_clcye = 0;
						wifi_send_flag = 0;
						
						send_time_delay = 0;
					}
				}
				
			}
			else
			{
				wifi_send_clcye = 0;
				wifi_send_flag = 0;
			}
		}
		
		/*------------------------------------------------------------------------------
                           RS232通信接口-协议处理
		  ----------------------------------------------------------------------------*/
		if(TRUE == If_com_recv_comp(COM_RS232))
		{ 
			comm_recv_buf.rx_num = If_com_get_recv_num(COM_RS232);
            If_com_get_recv_buf(COM_RS232 , comm_recv_buf.rx_buf , comm_recv_buf.rx_num);
			/* PCS 通信协议处理 */
			if(TRUE == pc_pace_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
			{
				If_com_send_str(COM_RS232 , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}
            /* 上位机通信协议 - 检测是否为升级指令 */
			else if(TRUE == is_iap_upgrade_cmd(comm_recv_buf.rx_buf , comm_recv_buf.rx_num))
			{
				comm_recv_buf.tx_num = iap_boot_reply_upgrade_cmd(comm_recv_buf.tx_buf , COM_PCS_BOOT_RATE , 1024);
				If_com_send_str(COM_RS232 , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
                iap_boot_jump_before_deal(BOOT_UPGRADE_RS232);
			}
            /* 上位机通信数据 - 读写命令 */
            else if(TRUE == parallel_com_recv_deal(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num , 1))
            {
                If_com_send_str(COM_RS232 , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
            }
			else if(TRUE == pc_jbd_com_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
			{
				If_com_send_str(COM_RS232 , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}
			else if(TRUE == jbd_modbus_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
			{
				If_com_send_str(COM_RS232 , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}
			else
			{
				;
			}
                
			var_set_data(VAR_ID_COMM_OFF_CNT , 0);
			var_set_data(VAR_ID_COMM_STATE , 0);
			
			com_reset_cnt[COM_RS232] = 0;
			com_init_cnt[COM_RS232] = 0;
			
			If_com_start_recv(COM_RS232);
		}
		
		/*------------------------------------------------------------------------------
                           GPS通信接口-协议处理
		  ----------------------------------------------------------------------------*/
		if(TRUE == If_com_recv_comp(COM_GPS))
		{ 
			comm_recv_buf.rx_num = If_com_get_recv_num(COM_GPS);
            If_com_get_recv_buf(COM_GPS , comm_recv_buf.rx_buf , comm_recv_buf.rx_num);
			/* PCS 通信协议处理 */
			if(TRUE == pc_pace_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
			{
				If_com_send_str(COM_GPS , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}
			else if(TRUE == pcs_rs485_comm_process(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
				If_com_send_str(COM_GPS , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
            /* 上位机通信协议 - 检测是否为升级指令 */
			else if(TRUE == is_iap_upgrade_cmd(comm_recv_buf.rx_buf , comm_recv_buf.rx_num))
			{
				comm_recv_buf.tx_num = iap_boot_reply_upgrade_cmd(comm_recv_buf.tx_buf , COM_PCS_BOOT_RATE , 1024);
				If_com_send_str(COM_GPS , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
                iap_boot_jump_before_deal(BOOT_UPGRADE_GPS);
			}
            /* 上位机通信数据 - 读写命令 */
            else if(TRUE == parallel_com_recv_deal(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num , 1))
            {
                If_com_send_str(COM_GPS , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
            }
			else if(TRUE == pc_jbd_com_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
			{
				If_com_send_str(COM_GPS , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}
			else if(TRUE == jbd_modbus_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
			{
				If_com_send_str(COM_GPS , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}
			else if(wifi_blue_com_analysis(comm_recv_buf.rx_buf , comm_recv_buf.rx_num , comm_recv_buf.tx_buf , &comm_recv_buf.tx_num))
			{
				If_com_send_str(COM_GPS , comm_recv_buf.tx_buf, comm_recv_buf.tx_num);
			}
			else
			{
				
			}
                
			var_set_data(VAR_ID_COMM_OFF_CNT , 0);
			var_set_data(VAR_ID_COMM_STATE , 0);
			
			com_reset_cnt[COM_GPS] = 0;
			com_init_cnt[COM_GPS] = 0;
			
			If_com_start_recv(COM_GPS);
		}
		
		/* 外部通信超时检测 */
		timeout_cnt = var_get_data(VAR_ID_COMM_OFF_CNT);
		if(var_get_data(VAR_ID_COMM_STATE) == 0)
		{
			timeout_cnt = var_get_data(VAR_ID_COMM_OFF_CNT);
			timeout_cnt += APP_COMM_TASK_CYCLE;
			// 30S通信超时 
			if(timeout_cnt >= 30000)
			{
				var_set_data(VAR_ID_COMM_STATE , 1);
			}
			var_set_data(VAR_ID_COMM_OFF_CNT , timeout_cnt);
		}
		else
		{
			
		}
		// 定时拉低方向引脚
		for( i = 0;i < COM_ID_MAX; ++ i)
		{
			com_reset_cnt[i] += APP_COMM_TASK_CYCLE;
			if(com_reset_cnt[i] >= 2000)
			{
				If_com_reset_recv(i);
				com_reset_cnt[i] = 0;
			}
		}
		
		// 定时重新初始化串口
		for( i = 0;i < COM_ID_MAX; ++ i)
		{
			com_init_cnt[i] += APP_COMM_TASK_CYCLE;
			if(com_init_cnt[i] >= 180000)  // 3min
			{
				switch(i)
				{
					case COM_PARALLEL:
						If_com_init_drv(COM_PARALLEL , COM_PARALLEL_RATE);
						break;
					case COM_PCS:
						If_com_init_drv(COM_PCS , pcs_rs485_get_com_rate());
						break;
					case COM_LCD:
						If_com_init_drv(COM_LCD , COM_LCD_RATE);
						break;
					case COM_BLUE:
						If_com_init_drv(COM_BLUE , COM_BLUE_RATE);
						break;
					case COM_RS232:
						If_com_init_drv(COM_RS232 , COM_BLUE_RATE);
						break;
					case COM_GPS:
						If_com_init_drv(COM_GPS , COM_BLUE_RATE);
						break;
				}
				com_init_cnt[i] = 0;
			}
		}
        task_run_count(TASK_ID_COM_COMM);
        
        vTaskDelay(APP_COMM_TASK_CYCLE);
    }
}

/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */

/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/
